﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WqLunTan.Areas.Manage.Helper
{
    /// <summary>
    /// 视图帮助类
    /// </summary>
    public static class ViewHelper
    {
        /// <summary>
        /// 标准表格css类
        /// </summary>
        public const string Standard_table_css = "table table-striped table-bordered table-hover dataTable";

        /// <summary>
        /// 标准无记录行
        /// </summary>
        public const string Standard_no_record_tr = "<tr><td colspan=\"100\" class=\"text-center\">暂无记录.</td></tr>";

        /// <summary>
        /// 无数据符号
        /// </summary>
        public const string NA_SYMBOL = "N/A";

        public static string TimeAgo(DateTimeOffset dt)
        {
            TimeSpan span = DateTimeOffset.UtcNow - dt;
            if (span.Days > 365)
            {
                int years = (span.Days / 365);
                if (span.Days % 365 != 0)
                    years += 1;
                return $"{years}年前";
            }
            if (span.Days > 30)
            {
                int months = (span.Days / 30);
                if (span.Days % 31 != 0)
                    months += 1;
                return $"{months}月前";
            }
            if (span.Days > 0)
                return $"{span.Days }天前";
            if (span.Hours > 0)
                return $"{span.Hours}小时前";
            if (span.Minutes > 0)
                return $"{span.Minutes}分钟前";
            if (span.Seconds > 5)
                return $"{span.Seconds}秒前";
            if (span.Seconds <= 5)
                return "刚刚";
            return string.Empty;
        }

        /// <summary>
        /// 获取表格头css类
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static string GetTableTheadClass(string columnName, Dictionary<string, string> para)
        {
            var result = "sorting";
            if (para != null && para.Count > 0 && para.ContainsKey("sort") && columnName == para["sort"])
            {
                result += $"_{para["order"].ToLower()}";
            }

            return result;
        }
    }
}
